package com.qianxx.base.utils;

import android.text.TextUtils;

import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil{

	/**
	 * 判断是否为中文
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isChineseWord(String str) {
		boolean temp = false;
		Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
		Matcher m = p.matcher(str);
		if (m.find()) {
			temp = true;
		}
		return temp;
	}

	/**
	 * 判断是否为数字
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		return pattern.matcher(str).matches();
	}

	/**
	 * 判断是否为英文
	 * @param str
	 * @return
	 */
	public static boolean isEnglish(String str) {
		Pattern p = Pattern.compile("[a-zA-Z]+");
		Matcher m = p.matcher(str);
		m = p.matcher(str);
		return m.matches();
	}
	/**
	 * 判断是否为标点符号
	 * @param str
	 * @return
	 */
	
	public static boolean isPunctuation(String str) {
		Pattern p = Pattern.compile( "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……& amp;*（）——+|{}【】‘；：”“’。，、？]");
		Matcher m = p.matcher(str);
		m = p.matcher(str);
		return m.matches();
	}
	public static boolean isUnderline(String str) {
		Pattern p = Pattern.compile("[*_*]");
		Matcher m = p.matcher(str);
		m = p.matcher(str);
		return m.matches();
	}

	public static boolean compile(String str) {
		Pattern p = Pattern.compile("[a-zA-Z0-9_\\u4e00-\\u9fa5]+");
		Matcher m = p.matcher(str);
		m = p.matcher(str);
		return m.matches();
	}

	/**
	 * 提供精确的小数位四舍五入处理�?
	 * 
	 * @param v
	 *            �?��四舍五入的数�?
	 * @param scale
	 *            小数点后保留几位
	 * @return 四舍五入后的结果
	 */
	public static int round(double v, int scale) {
		if (scale < 0) {
			throw new IllegalArgumentException(
					"The scale must be a positive integer or zero");
		}
		BigDecimal b = new BigDecimal(Double.toString(v));
		BigDecimal one = new BigDecimal("1");
		return (int)b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
	}
	/**
	 * 获取终端ip
	 * @return
	 */
	public static String getLocalIpAddress()  
    {  
        try  
        {  
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();)  
            {  
               NetworkInterface intf = en.nextElement();  
               for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();)  
               {  
                   InetAddress inetAddress = enumIpAddr.nextElement();  
                   if (!inetAddress.isLoopbackAddress())  
                   {  
                       return inetAddress.getHostAddress().toString();  
                   }  
               }  
           }  
        }  
        catch (SocketException ex)  
        {  
//            Log.e("WifiPreference IpAddress", ex.toString());
        }  
        return "";  
    }

	public final static ThreadLocal<SimpleDateFormat> dateFormater2 = new ThreadLocal<SimpleDateFormat>() {
		@Override
		protected SimpleDateFormat initialValue() {
			return new SimpleDateFormat("yyyy.MM.dd");
		}
	};
	/**
	 * 年龄处理 2014-11-26
	 *
	 * @param birthDay
	 * @return
	 */
	public static int age(String birthDay) {
		int a = 0;

		if (TextUtils.isEmpty(birthDay)) {
			a = 0;
			return a;
		}

		try {
			Date birDate = dateFormater2.get().parse(birthDay);
			Date nowDate = dateFormater2.get().parse(
					dateFormater2.get().format(new Date()));

			long days = (nowDate.getTime() - birDate.getTime())
					/ (24 * 60 * 60 * 1000);
			a = (int) (days / 365);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return a;
	}

	/**
	 * 创作时间
	 * @param birthDay
	 * @return
	 */
	public static int age(String birthDay, String time) {
		int a = 0;

		if (TextUtils.isEmpty(time)) {
			a = 0;
			return a;
		}

		try {
			Date birDate = dateFormater2.get().parse(birthDay);
			Date nowDate = dateFormater2.get().parse(time);

			long days = (nowDate.getTime() - birDate.getTime())
					/ (24 * 60 * 60 * 1000);
			a = (int) (days / 365);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return a;
	}
}
